*This file computes shift-share robustness following Goldsmith-Pinkham et al. (2020). See paper fore details.

*Install ado file bartik_weight_edit to your remote folder to excecute the code.
sysdir set PERSONAL "W:\ado"

*Set working directory with the samples created in the previous analysis for these robustness checks
cd "$temp"


/////////////////////////////////////////
*1950 agriculture exployment
//////////////////////////////////////
use bartik_50_c, clear

joinby  mun39 using municipality_shares_bartik_1939_all_munis


*create random x variable
egen x=group(mun39)


global municontrols  agrishare_1940  town city manushare_1940
drop if agrishare_1940==.


global shares share_ind4 share_ind19    ///
share_ind25  share_ind27  share_ind31  ///
share_ind32 share_ind33 share_ind34 share_ind35 ///
share_ind36 share_ind37 share_ind38 share_ind39
 
 
*run a regression just on the shares
reg primary50_individual $shares  $municontrols i.laani50 [aw=wei], r

preserve
matrix b = e(b)
matrix c = e(b)'
matrix list c
clear
svmat c 
gen ind="."

replace ind="4" if _n==1
replace ind="19" if _n==2
replace ind="25" if _n==3
replace ind="27" if _n==4
replace ind="31" if _n==5
replace ind="32" if _n==6
replace ind="33" if _n==7
replace ind="34" if _n==8
replace ind="35" if _n==9
replace ind="36" if _n==10
replace ind="37" if _n==11
replace ind="38" if _n==12
replace ind="39" if _n==13

tempfile bes
save `bes'
restore

local ind_stub share_ind
local y primary50_individual

bartik_weight_edit  , y(primary50_individual) x(x) z(`ind_stub'*) weightstub(repa_scaled*)  controls($municontrols)  absorb(laani50) weight_var(wei)

mat beta = r(beta)
mat alpha = r(alpha)
mat G = r(G)
qui desc `ind_stub'*, varlist
local varlist = r(varlist)

clear
svmat beta
svmat alpha
svmat G

gen ind = ""
local t = 1
foreach var in `varlist' {
	if regexm("`var'", "`ind_stub'(.*)") {
		qui replace ind = regexs(1) if _n == `t'
		}
	local t = `t' + 1
	}

rename 	G reparations_scaled
sum alpha1

drop beta*

joinby ind using  `bes'

sort alpha1

rename c1 beta

gen outcome="Agri1950"

*Here we have the the estimates from the shares. the rotemberg weights and reparation shares which are  the amount of reparations paid relative to 1000 employees. 

*sort by alphas and create table for these industries. 
export excel using "$temp\tables\GSS_alpha_agri1950_all.xls" in 9/13, replace firstrow(variables)


//////////////////////////////////////
*1970 incomes 
//////////////////////////////////////

use bartik_70_c, clear

joinby  mun39 using municipality_shares_bartik_1939_all_munis

*create random x variable
egen x=group(mun39)


*drop missing values
drop if agrishare_1940==.

gen ln_svatv=ln(svatv)

global shares share_ind4 share_ind19    ///
share_ind25  share_ind27  share_ind31  ///
share_ind32 share_ind33 share_ind34 share_ind35 ///
share_ind36 share_ind37 share_ind38 share_ind39
 
*run a regression just on the shares
reg ln_svatv $shares  $municontrols i.laani50 [aw=wei], r

preserve
matrix b = e(b)
matrix c = e(b)'
matrix list c
clear
svmat c 
gen ind="."
replace ind="4" if _n==1
replace ind="19" if _n==2
replace ind="25" if _n==3
replace ind="27" if _n==4
replace ind="31" if _n==5
replace ind="32" if _n==6
replace ind="33" if _n==7
replace ind="34" if _n==8
replace ind="35" if _n==9
replace ind="36" if _n==10
replace ind="37" if _n==11
replace ind="38" if _n==12
replace ind="39" if _n==13

tempfile bes
save `bes'
restore

local ind_stub share_ind
local y ln_svatv

bartik_weight_edit  , y(ln_svatv) x(x) z(`ind_stub'*) weightstub(repa_scaled*)  controls($municontrols)  absorb(laani50) weight_var(wei)

mat beta = r(beta)
mat alpha = r(alpha)
mat G = r(G)
qui desc `ind_stub'*, varlist
local varlist = r(varlist)

clear
svmat beta
svmat alpha
svmat G

gen ind = ""
local t = 1
foreach var in `varlist' {
	if regexm("`var'", "`ind_stub'(.*)") {
		qui replace ind = regexs(1) if _n == `t'
		}
	local t = `t' + 1
	}

rename 	G reparations_scaled
sum alpha1


drop beta*

joinby ind using  `bes'

sort alpha1

rename c1 beta

gen outcome="Logincome1970"

*So here we have the the estimates from the shares. the rotemberg weights and reparation shares which are  the amount of reparations paid relative to  employees. 

export excel using "$temp\tables\GSS_alpha_income1970.xls" in 9/13, replace firstrow(variables)

/////////////////////////////////////////
*Upward mobility 
//////////////////////////////////////

use "$temp\bartik_young", clear
 
joinby  mun39 using municipality_shares_bartik_1939_all_munis

reg income_rank $shares manushare_1940 $municontrols i.laani50 [aw=wei], r

preserve
matrix b = e(b)
matrix c = e(b)'
matrix list c
clear
svmat c 
gen ind="."
replace ind="4" if _n==1
replace ind="19" if _n==2
replace ind="25" if _n==3
replace ind="27" if _n==4
replace ind="31" if _n==5
replace ind="32" if _n==6
replace ind="33" if _n==7
replace ind="34" if _n==8
replace ind="35" if _n==9
replace ind="36" if _n==10
replace ind="37" if _n==11
replace ind="38" if _n==12
replace ind="39" if _n==13

tempfile bes
save `bes'
restore

egen x=group(mun39)
 
local ind_stub share_ind
local y income_rank

bartik_weight_edit  , y(income_rank) x(x) z(`ind_stub'*) weightstub(repa_scaled*)  controls($municontrols)  absorb(laani50) weight_var(wei)

mat beta = r(beta)
mat alpha = r(alpha)
mat G = r(G)
qui desc `ind_stub'*, varlist
local varlist = r(varlist)

clear
svmat beta
svmat alpha
svmat G

gen ind = ""
local t = 1
foreach var in `varlist' {
	if regexm("`var'", "`ind_stub'(.*)") {
		qui replace ind = regexs(1) if _n == `t'
		}
	local t = `t' + 1
	}

rename 	G reparations_scaled
sum alpha1

drop beta*

joinby ind using  `bes'

sort alpha1

rename c1 beta

gen outcome="Income Rank"

export excel using "$temp\tables\GSS_alpha_rank.xls" in 9/13, replace firstrow(variables)


